package com.gc.tpas.controller;

import com.gc.tpas.entity.QaInfo;
import com.gc.tpas.entity.QaSolution;
import com.gc.tpas.entity.QaType;
import com.gc.tpas.entity.QaUser;
import com.gc.tpas.service.QaInfoService;
import com.gc.tpas.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/5/7 0007.
 */
@Controller
@RequestMapping("/qa")
public class QaController {

    @Autowired
    QaInfoService qaInfoService;

    @RequestMapping("/showall")
    public String showAll(Model model){
        model.addAttribute("qalist",qaInfoService.getAllQa());
        return "main";
    }
    @RequestMapping("/detail/{id}")
    public String showdetail(@PathVariable("id") Integer id,Model model){
        model.addAttribute("qainfo",qaInfoService.showDetail(id));
        return "detail";
    }


    @RequestMapping("/getmyqa")
    public String getMyQaInfo(){

        return  "";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String addQa(QaInfo qaInfo, QaSolution qaSolution, HttpSession session){
        try {
            QaUser user =(QaUser) session.getAttribute("user");
            qaInfo.setPublisher(user);
            qaInfoService.addQa(qaInfo,qaSolution);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    @RequestMapping("/addtype")
    @ResponseBody
    public QaType addType(QaType qaType){
        try {
            qaType = qaInfoService.addType(qaType);
            return qaType;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addsolution")
    public String addSolution(QaSolution qaSolution,HttpSession session){
        QaUser user =(QaUser) session.getAttribute("user");
        qaSolution.setProvider(user);
        qaSolution.setMainflag(qaInfoService.existsMainSolution(qaSolution.getQaInfo().getId())?0:1);
        qaInfoService.addSolution(qaSolution);
        return "redirect:/qa/detail/"+qaSolution.getQaInfo().getId();
    }
    @Autowired
    UserService userService;
    @RequestMapping("/dingcai")
    @ResponseBody
    public String Ding(HttpSession session,int sid,int dingcai){
        QaUser user =(QaUser) session.getAttribute("user");
        int flag = userService.exsitDC(sid,user.getId());
        Integer result = null;
        if(flag==-1){
            try {
                result = userService.dingcai(sid,dingcai,user.getId());
            } catch (Exception e) {
                e.printStackTrace();
                return "服务器出现异常！请重试！";
            }
        }else{
            if(flag==0){
                return "您已经踩过了！";
            }else{
                return "您已经顶过了！";
            }
        }
        return result+"";
    }
}
